package com.decent.ejfadmin.report.dao;

import com.decent.ejfadmin.common.bean.ExkDailyReportBean;
import com.decent.ejfadmin.common.enums.ExkChannelPlat;
import com.decent.ejfadmin.report.dao.provider.CollectCardProfitDailyReportDaoProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 易销卡会员利润日报表
 *
 * @author starSky
 * @date 2/23/2018 7:01 PM.
 */
@Mapper
@Repository
public interface CollectCardProfitDailyReportDao {

    /**
     * 统计销卡日数据
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @Select("SELECT  S.productType,IFNULL(S.agentNo,'00000') as agentNo," +
            "       IFNULL(SUM(IF(S. status = 1, 1, 0)),0) AS successTotal, " +
            "       IFNULL(SUM(IF(S. status = 2, 1, 0)),0) AS failTotal, " +
            "       IFNULL(SUM(IF(S. status = 1, S.cost, 0)),0)  AS sales, " +
            "       IFNULL(SUM(IF(S. status = 1, S.settlePrice * S.userPrice, 0)),0) AS costs, " +
            "       IFNULL(SUM(IF(S. status = 1, S.settlePrice, 0)),0) AS totalFace, " +
            "       IFNULL(if(productType=1,0.03,0.015) * COUNT(1),0)  AS salesFee " +
            "  FROM yuhuang_supply_card S " +
            " WHERE S.endTime >= #{startTime} " +
            "   AND S.endTime <=   #{endTime} " +
            "  AND (S.failReason IN ('','23000','23001', '23003', '23005', '23002')) " +
            "   AND S.productType IN (1, 2, 3)" +
            "   AND `status` in(1,2)" +
            "  AND IF(failReason='23002',judgeFace IS NOT NULL,TRUE)  " +
            " GROUP BY S.productType,S.agentNo ")
    List<ExkDailyReportBean> dailyJobRecord(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 新增利润每日统计数据
     *
     * @param exkDailyReportBean 每日统计数据
     * @return
     */
    @Insert("INSERT INTO yuhuang_supply_card_profit  " +
            "  (DATE,  " +
            "   AGENTNO,  " +
            "   PRODUCTTYPE,  " +
            "   SUCCESSTOTAL,  " +
            "   FAILTOTAL,  " +
            "   SALES,  " +
            "   COSTS,  " +
            "   SALESFEE,  " +
            "   CHARGEFEE,  " +
            "   TOTALFACE,  " +
            "   PROFIT," +
            " XIANYUCOMMISSION)  " +
            "VALUES  " +
            "  (#{report.date},  " +
            "   #{report.agentNo},  " +
            "   #{report.productType},  " +
            "   #{report.successTotal},  " +
            "   #{report.failTotal},  " +
            "   #{report.sales},  " +
            "   #{report.costs},  " +
            "   #{report.salesFee},  " +
            "   #{report.chargeFee},  " +
            "   #{report.totalFace},  " +
            "   #{report.profit}," +
            "   #{report.xianyuCommission})  " +
            "   ON DUPLICATE KEY UPDATE   " +
            "   SUCCESSTOTAL = #{report.successTotal},   " +
            "   FAILTOTAL = #{report.failTotal},   " +
            "   SALES = #{report.sales},   " +
            "   COSTS = #{report.costs},   " +
            "   SALESFEE = #{report.salesFee},   " +
            "   CHARGEFEE = #{report.chargeFee},   " +
            "   TOTALFACE = #{report.totalFace},   " +
            "   PROFIT = #{report.profit}, " +
            "   XIANYUCOMMISSION = #{report.xianyuCommission}")
    int addReportRecord(@Param("report") ExkDailyReportBean exkDailyReportBean);

    /**
     * 销卡利润查询
     *
     * @param exkChannelPlat 销卡渠道
     * @param startTime      查询开始时间
     * @param endTime        查询结束时间
     * @return
     */
    @SelectProvider(type = CollectCardProfitDailyReportDaoProvider.class, method = "queryDailyReport")
    List<ExkDailyReportBean> queryDailyReport(@Param("channel") ExkChannelPlat exkChannelPlat, @Param("startTime") String startTime,
                                              @Param("endTime") String endTime);

    /**
     * 获取闲鱼佣金
     *
     * @param startTime
     * @param endTime
     * @param userId
     * @param exkDailyReportBean
     * @return
     */
    @Select("select sum(aa.commission/100) commission from yuhuang_xianyu_order_detail aa where bizOrderId in(\n" +
            "select DISTINCT batchId from yuhuang_supply_card bb where bb.userId=#{userId} and bb.status=1  \n" +
            "and endtime>=#{startTime} and endtime<=#{endTime} \n" +
            "            and bb.productType=#{exkDailyReport.productType} and if(bb.agentNo=00000,bb.agentNo='',#{exkDailyReport.agentNo}))" +
            " and transferTime>=#{startTime} and transferTime<=#{endTime} \n")
    BigDecimal getXianyuCommission(@Param("startTime") Date startTime,
                                   @Param("endTime") Date endTime, @Param("userId") Long userId, @Param("exkDailyReport") ExkDailyReportBean exkDailyReportBean);

}
